#pragma once
#ifndef _SCRIPT_VAL_H_
#define _SCRIPT_VAL_H_

#include "GameCom.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"


#include <iostream>
#include <map>

class FastJson;
struct lua_State;
typedef int (*lua_CFunction) (lua_State *L);
static void printstack(lua_State *L, const char *extra);

class ScriptVal
{
public:
	enum VAL_TYPE
	{
		SLUA_NIL = 0,
		SLUA_NUMBER = 1,
		SLUA_STRING = 2,
		SLUA_TABLE = 3,
		SLUA_BOOL = 4,
	};
	ScriptVal();
	~ScriptVal();

	void reset();
	void copy(const ScriptVal &val);

	void ClearTable(bool isDel = true);

	static int index(lua_State *L);
	static int SetVal(lua_State *L);
	static int NewLuaClass(lua_State *L);
	static int GC(lua_State *L);
	static int next(lua_State *L);
	static int nextIt(lua_State *L);
	static int tostring(lua_State *L);
	
	void push(std::string val);
	void push(double val);
	void push(bool val);
	void push(const ScriptVal *val);
	void push(std::string key, std::string val);
	void push(std::string key, double val);
	void push(std::string key, bool val);
	void push(std::string key, const char *val);
	void push(std::string key, ScriptVal *val);
	void StartLoop();
	ScriptVal* NextVal();

	int print(ScriptVal *val);

	uint8 type_ = SLUA_NIL;
	std::string name_;
	union {
		double d;
		std::string s;
		bool b;
	};

	typedef std::map<std::string, ScriptVal*> ScriptTable;
	ScriptTable table_;
	ScriptTable::iterator it_;

	static std::vector<std::tuple<const char *, lua_CFunction> > funvec;
};


#endif